﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Management;
using System.Threading;
using SharpPcap;
using PacketDotNet;


namespace UDPReceive
{
    class Program
    {
        public byte str_to_byte(string str_key)
        {
            char[] data = str_key.ToCharArray();
            int len = data.Length;
            int[] num = new Int32[len];
            int fg = 1;

            for (int i = 0; i < len; i++)
            {
                if ((data[i] >= '0') || (data[i] <= '9'))
                {
                    num[i] = (Convert.ToInt32(data[i])) - 0x30;

                }
                if ((data[i] >= 'A') || (data[i] <= 'F'))
                {
                    switch (data[i])
                    {
                        case 'A':
                            num[i] = 10;
                            break;
                        case 'B':
                            num[i] = 11;
                            break;
                        case 'C':
                            num[i] = 12;
                            break;
                        case 'D':
                            num[i] = 13;
                            break;
                        case 'E':
                            num[i] = 14;
                            break;
                        case 'F':
                            num[i] = 15;
                            break;
                    }
                }
            }
            for (int i = 0; i < len; i++)
            {
                if ((num[i] > 15) || (num[i] < 0))
                {

                    fg = 0;
                }
            }
            if (fg == 1)
            {
                return 0;
            }
            return 0;
        }
        public static string ByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString();
        }



        List<Byte[]> li = new List<byte[]>(1000);

        
        public void Receive()  
        {
            
            
            UdpClient sock;
            
            
  
            int port = 89;
            IPEndPoint ep;
            try
            {
                sock = new UdpClient(port);
                ep = new IPEndPoint(0, 89);
                do
                {
                    byte[] recData = sock.Receive(ref ep);
                    if (recData.Length > 0)
                    {
                        li.Add(recData);
                      //  Send();
                    }
                   // sock.Close();
                }
                while (true);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message.ToString());
              //  return;
            }


        }

        public void Send()
        {
            if (li.Count() <= 0)
                return;

            byte[] recData = li.ElementAt(0);

            byte[] macAdd = new byte[6];
            for (int i = 0; i < 6; i++)
            {
                macAdd[i] = recData[i];
            }

            byte[] temp = new byte[recData.Length - 6];


            for (int i = 0; i < recData.Length - 6; i++)
            {
                temp[i] = recData[i + 6];
            }


            string macAddress = ByteArrayToString(macAdd);
            Console.WriteLine("MacAddress from Client:" + macAddress);
            Console.WriteLine("Data from Client:" + Encoding.Default.GetString(temp));

            li.RemoveAt(0);

        }



        static void Main(string[] args)
        {
            Program p = new Program();
            Thread thReceive;
            
            //do
            //{
                Thread thSend;
                thReceive = new Thread(new ThreadStart(p.Receive));
                thSend = new Thread(new ThreadStart(p.Send));
                thReceive.Start();
                
                thSend.Start();
               // p.Send();
              //  Thread.Sleep(100);
            //}
            //while (true);

            //    UdpClient sock;
            //    int port = 89;
            //    IPEndPoint ep;
            //    try
            //    {
            //        sock = new UdpClient(port);
            //        ep = new IPEndPoint(0, 89);
            //        do
            //        {
            //            byte[] recData = sock.Receive(ref ep);


            //            byte[] macAdd = new byte[6];
            //            for (int i = 0; i < 6; i++)
            //            {
            //                macAdd[i] = recData[i];
            //            }

            //            byte[] temp = new byte[recData.Length - 6];


            //            for (int i = 0; i < recData.Length - 6; i++)
            //            {
            //                temp[i] = recData[i + 6];
            //            }


            //            string macAddress = ByteArrayToString(macAdd);
            //            Console.WriteLine("MacAddress : " + macAddress);
            //            Console.WriteLine("Data:" + Encoding.Default.GetString(temp));
            //        }
            //        while (true);
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.Write(ex.Message.ToString());
            //    }

            //}
        }
    }
}
